﻿using System;
using System.Web;
using Common;
using Dal;
using Entity.TaoBao;

public partial class Common_CallbackTB : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var code = Request.QueryString["code"];
        if (string.IsNullOrEmpty(code))
        {
            JsUtil.MessageBoxAndOpen("未取到code!", "/01soft.aspx", Page);
            return;
        }
        TaoBao tb = new TaoBao();
        AccessToken token = tb.GetAccessToken(code);
        //Response.Write(t.taobao_user_nick);
        DateTime expireTime = DateTime.Now.AddSeconds(token.expires_in);
        string ip;
        if (Context.Request.ServerVariables["HTTP_VIA"] != null) // 代理服务器
        {
            ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];// 得到真实的客户端地址 
        }
        else//如果没有使用代理服务器或者得不到客户端的
        {
            ip = Context.Request.ServerVariables["REMOTE_ADDR"];// 得到服务端的地址
        }
        string browser = Request.Browser.Type;
        string os = SystemCheck();
        string agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
        var users = new TaoBaoUser
                        {
                            TBuid = token.taobao_user_id,
                            nick = token.taobao_user_nick,
                            Token = token.access_token,
                            RefreshToken = token.refresh_token,
                            ExpireTime = expireTime
                        };
        int rows = users.Add(ip, browser, os, agent);
        if (rows <= 0) return;
        Response.Cookies["tbuid"].Value = token.taobao_user_id.ToString();
        Response.Cookies["nick"].Value = token.taobao_user_nick;
        Response.Cookies["token"].Value = token.access_token;
        Response.Cookies["usertype"].Value = token.taobao_user_nick == "xiumeiyinxiang" ? "a" : "u";//用户类型a为管理员u位卖家
        //Response.Cookies.Expires = DateTime.MaxValue; 
        //Mail m = new Mail();
        //m.Send(email, "感谢您注册！", string.Format("您的用户名是：{0},密码是：{1}。<br>赶快登陆吧！http://.com", nick, pw));
        //if (rows == 1)
        //    JsUtil.MessageBoxAndOpen("欢迎购买本应用！开始添加宝贝吧。", "addgoods.aspx", Page);
        //else
        Response.Redirect("/WeiBoTuiGUang/Intro.aspx");
    }
    //操作系统的版本信息:
    public static string SystemCheck()
    {
        string Agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
        if (Agent.IndexOf("NT 4.0") > 0)
        {
            return "Windows NT ";
        }
        else if (Agent.IndexOf("NT 5.0") > 0)
        {
            return "Windows 2000";
        }
        else if (Agent.IndexOf("NT 5.1") > 0)
        {
            return "Windows XP";
        }
        else if (Agent.IndexOf("NT 5.2") > 0)
        {
            return "Windows 2003";
        }
        else if (Agent.IndexOf("NT 6.0") > 0)
        {
            return "Windows Vista";
        }
        else if (Agent.IndexOf("NT 6.1") > 0)
        {
            return "Windows 7";
        }
        else if (Agent.IndexOf("NT 6.2") > 0)
        {
            return "Windows 8";
        }
        else if (Agent.IndexOf("WindowsCE") > 0)
        {
            return "Windows CE";
        }
        else if (Agent.IndexOf("NT") > 0)
        {
            return "Windows NT ";
        }
        else if (Agent.IndexOf("9x") > 0)
        {
            return "Windows ME";
        }
        else if (Agent.IndexOf("98") > 0)
        {
            return "Windows 98";
        }
        else if (Agent.IndexOf("95") > 0)
        {
            return "Windows 95";
        }
        else if (Agent.IndexOf("Win32") > 0)
        {
            return "Win32";
        }
        else if (Agent.IndexOf("Linux") > 0)
        {
            return "Linux";
        }
        else if (Agent.IndexOf("SunOS") > 0)
        {
            return "SunOS";
        }
        else if (Agent.IndexOf("Mac") > 0)
        {
            return "Mac";
        }
        else if (Agent.IndexOf("Linux") > 0)
        {
            return "Linux";
        }
        else if (Agent.IndexOf("Windows") > 0)
        {
            return "Windows";
        }
        return "unknow";
    }
}